//
//  LocalRepairHistoryDetailView.m
//  ICrEAM
//
//  Created by ysw on 2016/11/23.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "LocalRepairHistoryDetailView.h"
#import "LocalRepairHistoryDetailMaintenanceCell.h"
#import "LocalRepairHistoryDetailPartsCell.h"

@interface LocalRepairHistoryDetailView () <UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *maintenanceTableView;

@property (nonatomic, strong) UITableView *partsTableView;

@property (nonatomic, strong) NSMutableArray *model;

@property (nonatomic, strong) NSMutableArray *partsModel;

@property (nonatomic, strong) NSString *repairType;
@end

@implementation LocalRepairHistoryDetailView

- (instancetype)initWithModel:(ICMRepairOrderModel *)model andFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.model = model.TT_RO_LABOUR;
        self.partsModel = model.TT_RO_REPAIR_PART;
        self.repairType = model.RO_REPAIR_TYPE_NAME;
        self.backgroundColor = TextFeildBackgroundColor_OffWhite;
        NSDictionary *titleTextDic = @{@"font":TitleFont,@"color":TitleColor_Dark,@"align":@1};
        NSDictionary *priceTextDic = @{@"font":ContentFont,@"color":ContentTextColor_Gray,@"align":@1};
        NSDictionary *tableHeadTextDic = @{@"font":BoldContentFont,@"color":ContentTextColor_Gray,@"align":@1};
        
        //创建头部
        UIView *titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ActualHeight(46))];
        [self addSubview:titleView];
        
        UILabel *firstTitleLabel = [self createLabelWithDic:titleTextDic text:@"维修项目" frame:CGRectMake(ActualWidth(50), 0, ActualWidth(100),ActualHeight(46))];
        [titleView addSubview:firstTitleLabel];
        
        CGFloat price = [((ICMRepairMaintenanceModel *)[self.model PACA_ObjectAtIndex:0]).LABOUR_PRICE floatValue];
        UILabel *priceLabel = [self createLabelWithDic:priceTextDic text:[NSString stringWithFormat:@"工时单价：%.2f",price] frame:CGRectMake(ScreenWidth-ActualWidth(140), 0, ActualWidth(140), ActualHeight(46))];
        [titleView addSubview:priceLabel];
        
        UIView *firstSep = [[UIView alloc] initWithFrame:CGRectMake(0,ActualHeight(46)-1, ScreenWidth, 1)];
        firstSep.backgroundColor = SepColor_gray;
        [titleView addSubview:firstSep];
        
        //创建表头
        UIView *tableHeadView = [[UIView alloc]initWithFrame:CGRectMake(ActualWidth(170), titleView.bottomEdge+ActualHeight(20), ActualWidth(620), ActualHeight(20))];
        [self addSubview:tableHeadView];
        
        UILabel *nameLabel = [self createLabelWithDic:tableHeadTextDic text:@"项目名称/项目代码" frame:CGRectMake(0, 0, ActualWidth(200),ActualHeight(20))];
        [tableHeadView addSubview:nameLabel];
        
        UILabel *repairTypeLabel = [self createLabelWithDic:tableHeadTextDic text:@"维修类型" frame:CGRectMake(nameLabel.rightEdge, 0, ActualWidth(140),ActualHeight(20))];
        [tableHeadView addSubview:repairTypeLabel];
        
        UILabel *workPriceLabel = [self createLabelWithDic:tableHeadTextDic text:@"工时费" frame:CGRectMake(repairTypeLabel.rightEdge, 0, ActualWidth(140), ActualHeight(20))];
        [tableHeadView addSubview:workPriceLabel];
        
        UILabel *workTimeLabel = [self createLabelWithDic:tableHeadTextDic text:@"执行工时" frame:CGRectMake(workPriceLabel.rightEdge, 0, ActualWidth(140), ActualHeight(20))];
        [tableHeadView addSubview:workTimeLabel];
        
        //创建表
        NSInteger rowsNum = self.model.count;
        UITableView *maintenanceTableView = [[UITableView alloc]initWithFrame:CGRectMake(ActualWidth(170), tableHeadView.bottomEdge, ActualWidth(620), ActualHeight(50)*rowsNum)];
        maintenanceTableView.dataSource = self;
        maintenanceTableView.delegate = self;
        maintenanceTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [maintenanceTableView registerClass:[LocalRepairHistoryDetailMaintenanceCell class] forCellReuseIdentifier:@"DetailMaintenanceCell"];
        maintenanceTableView.tag = 0;
        maintenanceTableView.scrollEnabled = NO;
        maintenanceTableView.showsVerticalScrollIndicator = NO;
        maintenanceTableView.showsHorizontalScrollIndicator = NO;
        maintenanceTableView.backgroundColor = [UIColor clearColor];
        [self addSubview:maintenanceTableView];
        self.maintenanceTableView = maintenanceTableView;
        
        CGFloat fourthSepY = maintenanceTableView.bottomEdge;
        
        if (self.partsModel.count > 0) {
            //
            UIView *secondSep = [[UIView alloc] initWithFrame:CGRectMake(ActualWidth(170),maintenanceTableView.bottomEdge, ActualWidth(620), 1)];
            secondSep.backgroundColor = SepColor_gray;
            [self addSubview:secondSep];
            //
            UILabel *secondTitleLabel = [self createLabelWithDic:titleTextDic text:@"维修零件" frame:CGRectMake(ActualWidth(50), secondSep.bottomEdge, ActualWidth(100),ActualHeight(46))];
            [self addSubview:secondTitleLabel];
            
            //
            UIView *thirdSep = [[UIView alloc] initWithFrame:CGRectMake(ActualWidth(185),secondTitleLabel.bottomEdge, ScreenWidth-ActualWidth(185), 1)];
            thirdSep.backgroundColor = SepColor_gray;
            [self addSubview:thirdSep];
            
            //创建表头
            UIView *partsTableHeadView = [[UIView alloc]initWithFrame:CGRectMake(ActualWidth(170), thirdSep.bottomEdge+ActualHeight(20), ActualWidth(760), ActualHeight(20))];
            [self addSubview:partsTableHeadView];
            
            UILabel *partNameLabel = [self createLabelWithDic:tableHeadTextDic text:@"零件名称/零件代码" frame:CGRectMake(0, 0, ActualWidth(200),ActualHeight(20))];
            [partsTableHeadView addSubview:partNameLabel];
            
            UILabel *repairTypeLabel = [self createLabelWithDic:tableHeadTextDic text:@"维修类型" frame:CGRectMake(ActualWidth(200), 0, ActualWidth(140),ActualHeight(20))];
            [partsTableHeadView addSubview:repairTypeLabel];
            
            UILabel *sellAmountLabel = [self createLabelWithDic:tableHeadTextDic text:@"销售金额" frame:CGRectMake(repairTypeLabel.rightEdge, 0, ActualWidth(140), ActualHeight(20))];
            [partsTableHeadView addSubview:sellAmountLabel];
            
            UILabel *sellPriceLabel = [self createLabelWithDic:tableHeadTextDic text:@"销售单价" frame:CGRectMake(sellAmountLabel.rightEdge, 0, ActualWidth(140), ActualHeight(20))];
            [partsTableHeadView addSubview:sellPriceLabel];
            
            UILabel *quantityLabel = [self createLabelWithDic:tableHeadTextDic text:@"数量" frame:CGRectMake(sellPriceLabel.rightEdge, 0, ActualWidth(140), ActualHeight(20))];
            [partsTableHeadView addSubview:quantityLabel];
            
            //创建表
            NSInteger partsRowsNum = self.partsModel.count;
            UITableView *partsTableView = [[UITableView alloc]initWithFrame:CGRectMake(ActualWidth(170), partsTableHeadView.bottomEdge, ActualWidth(760), ActualHeight(50)*partsRowsNum)];
            partsTableView.dataSource = self;
            partsTableView.delegate = self;
            partsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            [partsTableView registerClass:[LocalRepairHistoryDetailPartsCell class] forCellReuseIdentifier:@"DetailPartsCell"];
            partsTableView.tag = 1;
            partsTableView.scrollEnabled = NO;
            partsTableView.showsVerticalScrollIndicator = NO;
            partsTableView.showsHorizontalScrollIndicator = NO;
            partsTableView.backgroundColor = [UIColor clearColor];
            [self addSubview:partsTableView];
            self.partsTableView = partsTableView;
            
            fourthSepY = partsTableView.bottomEdge;
        }
        
        UIView *fourthSep = [[UIView alloc] initWithFrame:CGRectMake(0,fourthSepY, ScreenWidth-ActualWidth(185), 1)];
        fourthSep.backgroundColor = BackgroundColor_White;
        [self addSubview:fourthSep];
        
    }
    return self;
}

+ (instancetype)createLocalRepairHistoryDetailViewWithModel:(ICMRepairOrderModel *)model andFrame:(CGRect)frame{
    return [[LocalRepairHistoryDetailView alloc]initWithModel:model andFrame:frame];
}

- (UILabel *)createLabelWithDic:(NSDictionary *)dic text:(NSString *)text frame:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.font = dic[@"font"];
    label.textColor = dic[@"color"];
    label.textAlignment = [dic[@"align"] integerValue];
    return label;
}

#pragma mark -- tableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 0) {
        return self.model.count;
    } else {
        return self.partsModel.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView.tag == 0) {
        LocalRepairHistoryDetailMaintenanceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailMaintenanceCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.repairType = self.repairType;
        [cell setupLabelsWithModel:self.model[indexPath.row]];
        return cell;
    } else {
        LocalRepairHistoryDetailPartsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailPartsCell"];
        cell.repairType = self.repairType;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setupLabelsWithModel:self.partsModel[indexPath.row]];
        return cell;
    }
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ActualHeight(50);
}
@end
